﻿
using Microsoft.Office.Interop.Word;
using System;

namespace RSSP1
{
    class HD_Word
    {
        public static string WORD_FileName = "RSSP-I.doc";
        public static void OpenWord(string fileName, System.Windows.Forms.RichTextBox richTextBox)
        {
         
            Application app = new Microsoft.Office.Interop.Word.Application();//可以打开word
            app.NormalTemplate.Saved = true;
            Document doc = null;      //需要记录打开的word

            object missing = System.Reflection.Missing.Value;
            object File = fileName  + WORD_FileName;
            object readOnly = false;//不是只读
            object isVisible = true;

            object unknow = Type.Missing;

            try
            {
                doc = app.Documents.Open(ref File, ref missing, ref readOnly,
                 ref missing, ref missing, ref missing, ref missing, ref missing,
                 ref missing, ref missing, ref missing, ref isVisible, ref missing,
                 ref missing, ref missing, ref missing);
                doc.ActiveWindow.Selection.WholeStory();//全选word文档中的数据
                doc.ActiveWindow.Selection.Copy();//复制数据到剪切板
                richTextBox.Paste();//richTextBox粘贴数据
            }
            catch (Exception e)
            {
                doc = null;
            }
            finally
            {
                //Object saveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdSaveChanges;
                //Object originalFormat = Type.Missing;
                //Object routeDocument = Type.Missing;
                app.Documents.Save();
                //app.Documents.Close();
                doc.Close(ref missing, ref missing, ref missing);
                //app.Quit(Type.Missing, Type.Missing, Type.Missing);
                //若已经没有文档存在，则关闭应用程序  
                if (app.Documents.Count == 0)
                {
                    app.Quit(Type.Missing, Type.Missing, Type.Missing);
                }

            }
        }
    }
}
